package com.culture.config.controller;

import java.util.List;

import javax.validation.Valid;

import com.culture.config.common.model.ResponseData;
import com.culture.config.common.util.ResponseUtil;
import com.culture.config.service.UserRoleService;
import com.culture.config.model.po.*;
import com.culture.config.model.vo.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api
@RestController
@RequestMapping("/api/user/role")
public class UserRoleController extends BaseController {
    @Autowired
    private UserRoleService userRoleService;

    @ApiOperation("page role menu")
	@PreAuthorize(value = "hasRole('ROLE_admin')")
	@PostMapping("/page")
    public ResponseData<List<UserRoleVO>> page(@RequestBody @Valid UserRolePageParam param){
        this.authenticationValidate();
        return ResponseUtil.successPage(this.userRoleService.page(param));
    }

    @ApiOperation("list role menu")
	@PreAuthorize(value = "hasRole('ROLE_admin')")
	@PostMapping("/list")
    public ResponseData<List<UserRoleVO>> listByRoleId(@RequestBody @Valid UserRoleListPO param){
        this.authenticationValidate();
        return ResponseUtil.success(this.userRoleService.listByUserId(param.getUserId()));
    } 

    @ApiOperation("update menu")
	@PreAuthorize(value = "hasRole('ROLE_root')")
	@PostMapping("/update")
    public ResponseData<String> update(@RequestBody @Valid UserRoleUpdatePO userRoleUpdatePO){
        this.authenticationValidate();
        this.userRoleService.update(userRoleUpdatePO);
        return ResponseUtil.success();
    }
}
